<?php

/**
 * This class handles the configuration of the plugin.
 *
 * @see config/bb_code_parser_config.yml
 * @see config/bb_code_parser_tags.yml
 *
 * @package    sfBBCodeParserPlugin
 * @author     COil
 * @since      1.0.0 - 29 avr 2009
 */

class sfBBCodeParserPluginConfigHandler extends sfYamlConfigHandler
{
  protected static
    $namespace = 'sfBBCodeParserPlugin';

  /**
   * Specific yml parse function.
   *
   * @param Array $configFiles
   * @return Array
   */
  public function execute($configFiles)
  {
    $config = self::parseYamls($configFiles);

    // compile data
    $retval = "<?php\n".
              "// This a cache config file for the sfBBCodeParserPlugin \n".
              "//\n".
              "// auto-generated by %s\n".
              "// date: %s\nreturn %s;\n".
              "//\n";

    $retval = sprintf($retval, __CLASS__, date('Y/m/d H:i:s'), var_export($config, true));

    return $retval;
  }

  /**
   * Load the config.
   *
   * @author COil
   * @since  6 august 08
   */
  public static function load()
  {
    $config = sfConfig::get(self::$namespace);

    if (!$config)
    {
      list($config, $tags) = self::checkAndGetConfig();
      
      sfConfig::set(self::$namespace, array_merge($config, $tags));
    }
  }

  /**
   * Generate the config file if not present and return a configuration for
   * a given key or return the default configuration.
   */
  public static function checkAndGetConfig()
  {
    return array(
      include(sfContext::getInstance()->getConfigCache()->checkConfig('config/bb_code_parser_config.yml')),
      include(sfContext::getInstance()->getConfigCache()->checkConfig('config/bb_code_parser_tags.yml'))
    );
  }

  /**
   * Set all filters for config.
   *
   * @author COil
   * @since  V1.0.0 - 29 apr 09
   */
  public static function applyConfig($config)
  {
  }

  // Basic getter / setter /////////////////////////////////////////////////////

  public static function getDefinedTagsForFilter($filter)
  { 
    $tags = self::getFilters();
    
    return $tags[$filter];
  }
  
  public static function getFilters()
  {
    $yaml = sfConfig::get('sfBBCodeParserPlugin');
    
    return $yaml['filters'];
  }

  public static function getConfig()
  {
    $yaml = sfConfig::get('sfBBCodeParserPlugin');
    
    return $yaml['config'];
  }

  public static function setConfig($config)
  {
    list($config_old, $tags) = self::checkAndGetConfig();
    
    sfConfig::set(self::$namespace, array_merge(array('config' => $config), $tags));
  }
}